package cn.online.taxi.api.passenger.controller;

import cn.online.taxi.common.pojo.Result;
import cn.online.taxi.common.utils.ResultUtil;
import cn.online.taxi.manager.dto.front.Passenger;
import cn.online.taxi.manager.dto.front.PassengerLoginRegister;
import cn.online.taxi.sso.service.LoginService;
import cn.online.taxi.sso.service.RegisterService;
import com.alibaba.dubbo.config.annotation.Reference;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

@RestController
@Api(value = "乘客注册登录")
public class SsoController {

    @Reference
    private RegisterService registerService;
    @Reference
    private LoginService loginService;

    @RequestMapping(value = "/passenger/register", method = RequestMethod.POST)
    @ApiOperation(value = "乘客注册")
    public Result<Object> passengerRegister(@RequestBody PassengerLoginRegister passengerLoginRegister) {
        int result = registerService.passengerRegister(passengerLoginRegister.getUserName(), passengerLoginRegister.getPhone(), passengerLoginRegister.getUserPwd());
        if(result == -1) {
            return new ResultUtil<Object>().setErrorMsg("用户名 密码 手机号不能为空");
        }else if(result == 0) {
            return new ResultUtil<Object>().setErrorMsg("手机号已被注册");
        }
        return new ResultUtil<Object>().setData(result);
    }

    @RequestMapping(value = "/passenger/login", method = RequestMethod.POST)
    @ApiOperation(value = "乘客登录")
    public Result<Passenger> passengerLogin(String phone, String userPwd) {
        Passenger passenger = loginService.passengerLogin(phone, userPwd);
        return new ResultUtil<Passenger>().setData(passenger);
    }

    @RequestMapping(value = "/passenger/checkLogin", method = RequestMethod.GET)
    @ApiOperation(value = "判断乘客是否登录")
    public Result<Object> checkLogin(@RequestParam(defaultValue = "") String token) {
        loginService.getPassengerToken(token);
        return new ResultUtil<Object>().setData(null);
    }

}
